package cn.web.controller;

import cn.constant.ApiConstant;
import cn.constant.Messages;
import cn.entity.Article;
import cn.entity.Tag;
import cn.service.ArticleService;
import cn.service.TagService;
import cn.util.AuthUtils;
import cn.web.model.ApiResult;
import cn.web.model.VmArticle;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author m on 2023/1/31
 * Description:
 */
@Api(tags = "标签业务")
@RestController
@RequestMapping("/tag")
public class TagApiController {

    private final TagService tagService;
    private final ArticleService articleService;

    @Autowired
    public TagApiController(TagService tagService, ArticleService articleService) {
        this.tagService = tagService;
        this.articleService = articleService;
    }

    //新建tag
    @ApiOperation("新建标签")
    @PostMapping(value = "/create",produces = ApiConstant.API_PRODUCES)
    public ApiResult createTag(@RequestBody JSONObject json){
        Tag tag = new Tag();
        tag.setName(json.getString("name"));
        //如果是管理员则直接创建
        if(AuthUtils.getUser(json.getString(ApiConstant.KEY_TOKEN)).getType() == 0){
            tag.setOpen(true);
        }
        tagService.createTag(tag);
        return new ApiResult(true, Messages.SUCCESS.toString(),tag);
    }

    //删除tag/拒绝tag
    @ApiOperation("删除标签")
    @DeleteMapping(value = "/delete",produces = ApiConstant.API_PRODUCES)
    public ApiResult deleteTag(@RequestBody JSONObject json){
        Tag tag = tagService.getTagById(UUID.fromString(json.getString("id")));
        Messages msg = tagService.deleteTag(tag);
        if (Messages.SUCCESS.equals(msg)) {
            return new ApiResult(true,msg.toString(),null);
        }else{
            return new ApiResult(false,msg.toString(),null);
        }
    }

    //条件查找tag
    @ApiOperation("条件查找标签")
    @GetMapping(value = "/search/{kw}")
    public ApiResult searchTag(@PathVariable String kw){
        List<Tag> tags = tagService.searchTags(kw);
        return new ApiResult(true, Messages.SUCCESS.toString(),tags);
    }

    //查找某个tag下的文章
    @ApiOperation("查找标签的文章")
    @GetMapping(value = "/find/{id}")
    public ApiResult searchArticlesById(@PathVariable("id")UUID id){
        List<VmArticle> articles = new ArrayList<>();
        Tag tag = tagService.getTagById(id);
        tag.getArticles().forEach(article -> articles.add(VmArticle.create(article)));

        return new ApiResult(true, Messages.SUCCESS.toString(),articles);
    }

//    查找未审批tag
    @ApiOperation("查找为审批标签")
    @GetMapping(value = "/findIsOpen")
    public ApiResult findTags(@RequestParam String token){
        List<Tag> tags = tagService.findTags();
        return new ApiResult(true,Messages.SUCCESS.toString(),tags);
    }

    //同意tag
    @ApiOperation("同意标签申请")
    @PostMapping(value = "/approve/{id}")
    public ApiResult approveTag(@PathVariable("id")UUID id,@RequestParam String token){
        Tag tag =tagService.getTagById(id);
        if (tag == null) {
            return new ApiResult(false, Messages.WRONG_ID.toString(),null);
        }
        tag.setOpen(true);
        tagService.createTag(tag);
        return new ApiResult(true,Messages.SUCCESS.toString(),tag);
    }

    @PostMapping(value = "/insert")
    public ApiResult insertTag(@RequestBody JSONObject json){
        Article article = articleService.getById(UUID.fromString(json.getString("article_id")));
        if (article == null) {
            return new ApiResult(false, Messages.WRONG_ID.toString(),null);
        }
        //获取标签 标签有可能为null,需要判断
        List<Object> list = json.getJSONArray("tags");
        List<Tag> tags = new ArrayList<>();
        list.forEach(o -> {
            if(tagService.getTagByName((String) o)!=null){
                tags.add(tagService.getTagByName((String) o));
            }
        });
        article.setTags(tags);
        articleService.createArticle(article);
        return new ApiResult(true, Messages.SUCCESS.toString(),VmArticle.create(article));
    }

}
